
/**   
*    
* 项目名称：a_yongan   
* 类名称：WarehouseController   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年6月17日 下午8:25:39    
* 修改时间：2018年6月17日 下午8:25:39   
* 修改备注：   
* @version    
*    
*/
package com.yongan.production.web;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yongan.production.entity.Details;
import com.yongan.production.entity.ProductStructureList;
import com.yongan.production.entity.ProductionPlan;
import com.yongan.production.service.DetailsService;
import com.yongan.production.service.ProductStructureListService;
import com.yongan.production.service.ProductionPlanService;
import com.yongan.sales.entity.SalesOrder;
import com.yongan.sales.service.SalesOrderService;
import com.yongan.user.dto.UserResult;
import com.yongan.util.Execution;
import com.yongan.util.PageBean;
import com.yongan.util.SerialNumber;
import com.yongan.util.TreeForGoodsinformation;
import com.yongan.warehouse.entity.GoodType;
import com.yongan.warehouse.entity.Goodsinformation;
import com.yongan.warehouse.service.GoodTypeService;
import com.yongan.warehouse.service.GoodsinformationService;

/**
 * @author liubo
 *
 */
@Controller
@RequestMapping("/productionplan")
public class ProductionPlanController {

	@Autowired
	private ProductionPlanService productionPlanService;
	@Autowired
	private SalesOrderService salesOrderService;
	@Autowired
	private GoodTypeService goodTypeService;
	@Autowired
	private GoodsinformationService goodsinformationService;
	@Autowired
	private ProductStructureListService listService;
	@Autowired
	private DetailsService detailsService;
	@RequestMapping(value = "/productionplanlist", method = RequestMethod.GET)
	public String productionplancontent(Model model,HttpServletRequest request) {
		List<ProductionPlan> productionplanlist = null;
		String page = request.getParameter("currentPage");
		int currentPage;
		int totalRecords;
		if (page != null) {
			currentPage = Integer.parseInt(page);
		} else {
			currentPage = 1;
		}
		PageBean<ProductionPlan> pb = new PageBean<ProductionPlan>();
		
		totalRecords = productionPlanService.getCount();
		pb.setCurrentPage(currentPage);
		pb.setTotalRecords(totalRecords);
		pb.setTotalPages(pb.getTotalRecords() % pb.getPagesize() == 0 ? pb
				.getTotalRecords() / pb.getPagesize() : pb.getTotalRecords()
				/ pb.getPagesize() + 1);
		if (totalRecords > 0) {
			int start = (pb.getCurrentPage() - 1) * pb.getPagesize();
			int end = pb.getCurrentPage() * pb.getPagesize();
			if (end > totalRecords - 1) {
				end = totalRecords;
			}
			productionplanlist = productionPlanService.getProductionPlan(start, end);
			//model.addAttribute("pb", pb);
		}
		model.addAttribute("pb", pb);
		model.addAttribute("productionplanlist", productionplanlist);
		return "productionplanlist";
	}
	@RequestMapping(value = "/{productionplanId}/detail", method = RequestMethod.GET)
	public String productionplandetail(@PathVariable("productionplanId") String productionplanId, Model model,HttpServletRequest request) {
		List<SalesOrder> salesOrderlist = null;
		String page = request.getParameter("currentPage");
		
		String dialog = request.getParameter("dialog");//为选择订货单单独增加
		int currentPage;
		int totalRecords;
		if (page != null) {
			currentPage = Integer.parseInt(page);
		} else {
			currentPage = 1;
		}
		PageBean<SalesOrder> pb = new PageBean<SalesOrder>();
		
		totalRecords = salesOrderService.getCount();
		pb.setCurrentPage(currentPage);
		pb.setTotalRecords(totalRecords);
		pb.setTotalPages(pb.getTotalRecords() % pb.getPagesize() == 0 ? pb
				.getTotalRecords() / pb.getPagesize() : pb.getTotalRecords()
				/ pb.getPagesize() + 1);
		if (totalRecords > 0) {
			int start = (pb.getCurrentPage() - 1) * pb.getPagesize();
			int end = pb.getCurrentPage() * pb.getPagesize();
			if (end > totalRecords - 1) {
				end = totalRecords;
			}
			salesOrderlist = salesOrderService.getSalesOrderList(start, end);
			//model.addAttribute("pb", pb);
		}
		
		List<GoodType> goodTypeList = goodTypeService.getAllTree();
		List<Goodsinformation> goodsinformationTree = goodsinformationService.getAllGoodsinformationList();
		List<TreeForGoodsinformation> treeList = new ArrayList<TreeForGoodsinformation>();
		for(Goodsinformation goodsinformation:goodsinformationTree){
			TreeForGoodsinformation tree = new TreeForGoodsinformation();
			tree.setId(goodsinformation.getGoodsinformationNumber());
			tree.setName(goodsinformation.getGoodsinformationHpmc());
			tree.setHpxh(goodsinformation.getGoodsinformationHpxh());
			tree.setDw(goodsinformation.getGoodsinformationHpdw());
			tree.setDj(goodsinformation.getGoodsinformationHpdj());
			tree.setpId(goodsinformation.getGoodsinformationHplbid());
			tree.setRid(goodsinformation.getGoodsinformationId());
			tree.setType(goodsinformation.getGoodsinformationHplb());
			treeList.add(tree);
		}
		for(GoodType goodType:goodTypeList){
			TreeForGoodsinformation tree = new TreeForGoodsinformation();
			tree.setName(goodType.getGoodtypeMc());
			tree.setId(goodType.getGoodtypeId());
			tree.setNocheck(true);
			treeList.add(tree);
		}
		JSONArray jsonObject = JSONArray.fromObject(treeList);
		String json = jsonObject.toString();
		model.addAttribute("goodinformationTree", json);
		
		model.addAttribute("pb", pb);
		model.addAttribute("salesOrderlist", salesOrderlist);
		if (!(productionplanId.equals("")|productionplanId==null)) {
			ProductionPlan productionplan = productionPlanService.getProductionPlanById(productionplanId);
			model.addAttribute("productionplan", productionplan);
			//System.out.println(productionplan.getProductstructurelistId()+"-------");
			List<Details> detailsall = detailsService.getDetailsList(productionplan.getProductstructurelistId());
			//System.out.println(detailsall);
			model.addAttribute("detailsall", detailsall);
		}
		return "productionplandetail";
	}
	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> productionplanupdate(ProductionPlan productionPlan, Model model) {
		System.out.println(productionPlan);
		Execution rs = productionPlanService.executeProductionPlan(productionPlan, productionPlan.getProductionPlanId(), "123321231");
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<Execution>(true, rs);
		}
		return new UserResult<Execution>(false, "");
	}
	@RequestMapping(value = "/{productionplanId}/delete", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> productionplanDelete(
			@PathVariable("productionplanId") String productionplanId, Model model) {
		if (!(productionplanId.equals("")|productionplanId==null)) {
			Execution rsdelete = productionPlanService.executeProductionPlan(null, productionplanId, "1212331232");
			int rsn = rsdelete.getState();
			if (rsn == 1) {
				return new UserResult<Execution>(true, rsdelete);
			}
		}
		return new UserResult<Execution>(false, "");
	}
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String productionplanadd(Model model,HttpServletRequest request) {
		String number = SerialNumber.Getnum();
		number = "SCJH"+number;
		model.addAttribute("number", number);
		List<SalesOrder> salesOrderlist = null;
		String page = request.getParameter("currentPage");
		
		String dialog = request.getParameter("dialog");//为选择订货单单独增加
		System.out.println(dialog+"=============");
		int currentPage;
		int totalRecords;
		if (page != null) {
			currentPage = Integer.parseInt(page);
		} else {
			currentPage = 1;
		}
		PageBean<SalesOrder> pb = new PageBean<SalesOrder>();
		
		totalRecords = salesOrderService.getCount();
		pb.setCurrentPage(currentPage);
		pb.setTotalRecords(totalRecords);
		pb.setTotalPages(pb.getTotalRecords() % pb.getPagesize() == 0 ? pb
				.getTotalRecords() / pb.getPagesize() : pb.getTotalRecords()
				/ pb.getPagesize() + 1);
		if (totalRecords > 0) {
			int start = (pb.getCurrentPage() - 1) * pb.getPagesize();
			int end = pb.getCurrentPage() * pb.getPagesize();
			if (end > totalRecords - 1) {
				end = totalRecords;
			}
			salesOrderlist = salesOrderService.getSalesOrderList(start, end);
			//model.addAttribute("pb", pb);
		}
		
		List<GoodType> goodTypeList = goodTypeService.getAllTree();
		List<Goodsinformation> goodsinformationTree = goodsinformationService.getAllGoodsinformationList();
		List<TreeForGoodsinformation> treeList = new ArrayList<TreeForGoodsinformation>();
		for(Goodsinformation goodsinformation:goodsinformationTree){
			TreeForGoodsinformation tree = new TreeForGoodsinformation();
			tree.setId(goodsinformation.getGoodsinformationNumber());
			tree.setName(goodsinformation.getGoodsinformationHpmc());
			tree.setHpxh(goodsinformation.getGoodsinformationHpxh());
			tree.setDw(goodsinformation.getGoodsinformationHpdw());
			tree.setDj(goodsinformation.getGoodsinformationHpdj());
			tree.setpId(goodsinformation.getGoodsinformationHplbid());
			tree.setRid(goodsinformation.getGoodsinformationId());
			tree.setType(goodsinformation.getGoodsinformationHplb());
			treeList.add(tree);
		}
		for(GoodType goodType:goodTypeList){
			TreeForGoodsinformation tree = new TreeForGoodsinformation();
			tree.setName(goodType.getGoodtypeMc());
			tree.setId(goodType.getGoodtypeId());
			tree.setNocheck(true);
			treeList.add(tree);
		}
		JSONArray jsonObject = JSONArray.fromObject(treeList);
		String json = jsonObject.toString();
		model.addAttribute("goodinformationTree", json);
		
		model.addAttribute("pb", pb);
		model.addAttribute("salesOrderlist", salesOrderlist);
		return "productionplanadd";
	}
	@RequestMapping(value = "/adddo", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> productionplanadddo(ProductionPlan productionPlan, Model model) {
		productionPlan.setProductionPlanId(UUID.randomUUID().toString());
		Execution rs = productionPlanService.executeProductionPlan(productionPlan, "", "1233213");
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<Execution>(true, rs);
		}
		return new UserResult<Execution>(false, "");
	}
	/**
	 * 返回产品结构清单树
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{productionId}/value", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<ProductStructureList> productionvalue(
			@PathVariable("productionId") String productionId, Model model) {
		if (!(productionId.equals("")|productionId==null)) {
			ProductStructureList list = listService.getProductStructureListByGoodsId(productionId);
			if (list != null) {
				return new UserResult<ProductStructureList>(true, list);
			}else{
				return new UserResult<ProductStructureList>(false, "");
			}
		}
		return new UserResult<ProductStructureList>(false, "");
	}
	
	@RequestMapping(value = "/{productionPlanId}/{productstructurelistId}/lingliao", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> productionplanLingliao(
			@PathVariable("productionPlanId") String productionPlanId,
			@PathVariable("productstructurelistId") String productstructurelistId, Model model) {
		if (!(productstructurelistId.equals("")|productstructurelistId==null)) {
			List<Details> detailsall = detailsService.getDetailsList(productstructurelistId);
			Execution rsdelete = productionPlanService.executeLingLiao(productionPlanId,detailsall, "1212331232");
			int rsn = rsdelete.getState();
			if (rsn == 1) {
				return new UserResult<Execution>(true, rsdelete);
			}
		}
		return new UserResult<Execution>(false, "");
	}
}
